/*
 * buffer_detect.hpp
 *
 *  Created on: 2016年4月8日
 *      Author: win7
 */



	bool detect(int32_t offset, char* buf, int32_t length){
		return detect(offset, (unsigned char*)(buf), length);
	}
	bool detect(int32_t offset, unsigned char& value){
		return detect(offset, static_cast<unsigned char*>(&value), sizeof(value));
	}
	bool detect(int32_t offset, unsigned short& value){
		if(detect(offset, (unsigned char*)(&value), sizeof(value))){
			value = ::ntohs(value);
			return true;
		}
		return false;
	}
	bool detect(int32_t offset, unsigned int& value){
		switch(sizeof(value)){
		case 2:
			return detect(offset, (unsigned short &)(value));
		case 4:
			if(detect(offset, (unsigned char*)(&value), sizeof(value))){
				value = ::ntohl(static_cast<uint32_t>(value));
				return true;
			}
			return false;
		case 8:
			return detect(offset, (unsigned long long &)(value));
		default:
			return false;
		}
	}
	bool detect(int32_t offset, unsigned long& value){
		switch(sizeof(value)){
		case 4:
			if(detect(offset, (unsigned char*)(&value), sizeof(value))){
				value = ::ntohl(static_cast<uint32_t>(value));
				return true;
			}
			return false;
		case 8:
			return detect(offset, (unsigned long long &)(value));
		default:
			return false;
		}
	}
	bool detect(int32_t offset, unsigned long long& value){
		if(detect(offset, (unsigned char*)(&value), sizeof(value))){
			value = (static_cast<uint64_t>(::ntohl(static_cast<uint32_t>(value&0xFFFFFFFF))) << 32) | ::ntohl(static_cast<uint32_t>((value >> 32)&0xFFFFFFFF));
			return true;
		}
		return false;
	}

	bool detect(int32_t offset, signed char& value){
		return detect(offset, (unsigned char*)(&value), sizeof(value));
	}
	bool detect(int32_t offset, signed short& value){
		return detect(offset, (unsigned short &)(value));
	}
	bool detect(int32_t offset, signed int& value){
		return detect(offset, (unsigned int &)(value));
	}
	bool detect(int32_t offset, signed long& value){
		return detect(offset, (unsigned long &)(value));
	}
	bool detect(int32_t offset, signed long long& value){
		return detect(offset, (unsigned long long &)(value));
	}

